/*
 * Created by JFormDesigner on Thu Jun 15 20:05:41 CST 2023
 */

package fm.warehousing;

import java.awt.event.*;
import dao.Dao_warehousing;
import model.Goods;
import model.Ware;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;

/**
 * @author YangC
 */
public class Look extends JInternalFrame {

    String [] columns = {"商品编号", "商品名称", "类别", "单价", "数量","码洋","折扣","实洋"};
    private Object [][] getFS(List list)
    {
        Object [][] results = new Object[list.size()][columns.length];

        for(int i = 0; i < list.size(); i++)
        {
            Goods goods = (Goods) list.get(i);

            results[i][0] = goods.getGoodsID();
            results[i][1] = goods.getGoodsName();
            results[i][2] = goods.getType();
            results[i][3] = goods.getPrice();
            results[i][4] = goods.getNumber();
            results[i][5] = goods.getTotal();
            results[i][6] = goods.getDiscount();
            results[i][7] = goods.getReal();
        }


        return results;
    }

    public Look() {
        initComponents();
        iniData();
        iniData_1();
    }
    public void iniData()
    {
        Vector<String> its = Dao_warehousing.getWareID();
        DefaultComboBoxModel<String> model = new DefaultComboBoxModel<>(its);
        comboBox1.setModel(model);

        String wareID = (String) comboBox1.getSelectedItem();

        Object [][] results = getFS(Dao_warehousing.select_goods(wareID));
        DefaultTableModel model_1 = new DefaultTableModel(results, columns);
        table1.setModel(model_1);

        int setRow = table1.getRowCount();
        int goods_counts = 0;
        double goods_reals = 0.0;
        double goods_totals = 0.0;

        for(int i = 0; i < setRow; i++)
        {
            int counts = Integer.parseInt(table1.getValueAt(i,4).toString().trim());
            double totals = Double.parseDouble(table1.getValueAt(i, 5).toString().trim());
            double reals = Double.parseDouble(table1.getValueAt(i, 7).toString().trim());

            goods_counts += counts;
            goods_reals += reals;
            goods_totals += totals;

        }

        double goods_discounts = goods_totals - goods_reals;
        countTextField.setText(String.valueOf(goods_counts));
        discountsTextField.setText(String.valueOf(goods_discounts));
        realTextField.setText(String.valueOf(goods_reals));
        totalTextField.setText(String.valueOf(goods_totals));
    }

    public void iniData_1()
    {
        String wareID = (String) comboBox1.getSelectedItem();

        Object [][] results = getFS(Dao_warehousing.select_goods(wareID));
        DefaultTableModel model_1 = new DefaultTableModel(results, columns);
        table1.setModel(model_1);

        int setRow = table1.getRowCount();
        int goods_counts = 0;
        double goods_reals = 0.0;
        double goods_totals = 0.0;

        for(int i = 0; i < setRow; i++)
        {
            int counts = Integer.parseInt(table1.getValueAt(i,4).toString().trim());
            double totals = Double.parseDouble(table1.getValueAt(i, 5).toString().trim());
            double reals = Double.parseDouble(table1.getValueAt(i, 7).toString().trim());

            goods_counts += counts;
            goods_reals += reals;
            goods_totals += totals;

        }

        double goods_discounts = goods_totals - goods_reals;
        countTextField.setText(String.valueOf(goods_counts));
        discountsTextField.setText(String.valueOf(goods_discounts));
        realTextField.setText(String.valueOf(goods_reals));
        totalTextField.setText(String.valueOf(goods_totals));

        Ware list = Dao_warehousing.select_All(wareID);

        wareIDTextField.setText(list.getWareID());
        sIDTextField.setText(list.getSupplierID());
        sNameTextField.setText(list.getSupplierName());
        sPeopleTextField.setText(list.getSupplierMan());
        phoneTextField.setText(list.getsPhone());
        addressTextField.setText(list.getAddress());
        timeTextField.setText(list.getTime());
    }

    private void cancel(ActionEvent e) {
        // TODO add your code here
        doDefaultCloseAction();
    }

    private void comboBox1(ActionEvent e) {
        String wareID = (String) comboBox1.getSelectedItem();

        Object [][] results = getFS(Dao_warehousing.select_goods(wareID));
        DefaultTableModel model_1 = new DefaultTableModel(results, columns);
        table1.setModel(model_1);

        int setRow = table1.getRowCount();
        int goods_counts = 0;
        double goods_reals = 0.0;
        double goods_totals = 0.0;

        for(int i = 0; i < setRow; i++)
        {
            int counts = Integer.parseInt(table1.getValueAt(i,4).toString().trim());
            double totals = Double.parseDouble(table1.getValueAt(i, 5).toString().trim());
            double reals = Double.parseDouble(table1.getValueAt(i, 7).toString().trim());

            goods_counts += counts;
            goods_reals += reals;
            goods_totals += totals;

        }

        double goods_discounts = goods_totals - goods_reals;
        countTextField.setText(String.valueOf(goods_counts));
        discountsTextField.setText(String.valueOf(goods_discounts));
        realTextField.setText(String.valueOf(goods_reals));
        totalTextField.setText(String.valueOf(goods_totals));

        Ware list = Dao_warehousing.select_All(wareID);

        wareIDTextField.setText(list.getWareID());
        sIDTextField.setText(list.getSupplierID());
        sNameTextField.setText(list.getSupplierName());
        sPeopleTextField.setText(list.getSupplierMan());
        phoneTextField.setText(list.getsPhone());
        addressTextField.setText(list.getAddress());
        timeTextField.setText(list.getTime());
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel1 = new JPanel();
        cancelButton = new JButton();
        panel7 = new JPanel();
        panel8 = new JPanel();
        comboBox1 = new JComboBox();
        panel9 = new JPanel();
        label6 = new JLabel();
        wareIDTextField = new JTextField();
        label7 = new JLabel();
        sIDTextField = new JTextField();
        label8 = new JLabel();
        sNameTextField = new JTextField();
        label9 = new JLabel();
        sPeopleTextField = new JTextField();
        label10 = new JLabel();
        phoneTextField = new JTextField();
        label11 = new JLabel();
        addressTextField = new JTextField();
        label12 = new JLabel();
        countTextField = new JTextField();
        label13 = new JLabel();
        totalTextField = new JTextField();
        label14 = new JLabel();
        realTextField = new JTextField();
        label15 = new JLabel();
        discountsTextField = new JTextField();
        label16 = new JLabel();
        timeTextField = new JTextField();
        panel10 = new JPanel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();

        //======== this ========
        setVisible(true);
        setClosable(true);
        setIconifiable(true);
        setTitle("sunny\u5165\u5e93\u6e05\u5355");
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //======== panel1 ========
        {
            panel1.setMinimumSize(new Dimension(397, 30));
            panel1.setPreferredSize(new Dimension(397, 45));
            panel1.setLayout(null);

            //---- cancelButton ----
            cancelButton.setText("\u8fd4\u56de");
            cancelButton.addActionListener(e -> cancel(e));
            panel1.add(cancelButton);
            cancelButton.setBounds(new Rectangle(new Point(330, 9), cancelButton.getPreferredSize()));

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel1.getComponentCount(); i++) {
                    Rectangle bounds = panel1.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel1.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel1.setMinimumSize(preferredSize);
                panel1.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel1);
        panel1.setBounds(0, 509, 738, panel1.getPreferredSize().height);

        //======== panel7 ========
        {
            panel7.setPreferredSize(new Dimension(0, 250));
            panel7.setLayout(new BorderLayout());

            //======== panel8 ========
            {
                panel8.setPreferredSize(new Dimension(310, 55));
                panel8.setBorder(new TitledBorder("\u8bf7\u9009\u62e9\u5165\u5e93\u5355\u7f16\u53f7"));
                panel8.setLayout(null);

                //---- comboBox1 ----
                comboBox1.addActionListener(e -> comboBox1(e));
                panel8.add(comboBox1);
                comboBox1.setBounds(265, 20, 190, comboBox1.getPreferredSize().height);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel8.getComponentCount(); i++) {
                        Rectangle bounds = panel8.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel8.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel8.setMinimumSize(preferredSize);
                    panel8.setPreferredSize(preferredSize);
                }
            }
            panel7.add(panel8, BorderLayout.NORTH);

            //======== panel9 ========
            {
                panel9.setBorder(new TitledBorder("\u5165\u5e93\u4fe1\u606f"));
                panel9.setLayout(null);

                //---- label6 ----
                label6.setText("\u5165\u5e93\u5355\u7f16\u53f7\uff1a");
                panel9.add(label6);
                label6.setBounds(29, 35, 85, 25);

                //---- wareIDTextField ----
                wareIDTextField.setEditable(false);
                panel9.add(wareIDTextField);
                wareIDTextField.setBounds(105, 33, 94, 31);

                //---- label7 ----
                label7.setText("\u4f9b\u5e94\u5546\u7f16\u53f7\uff1a");
                panel9.add(label7);
                label7.setBounds(223, 38, label7.getPreferredSize().width, 21);

                //---- sIDTextField ----
                sIDTextField.setEditable(false);
                panel9.add(sIDTextField);
                sIDTextField.setBounds(299, 34, 102, 30);

                //---- label8 ----
                label8.setText("\u4f9b\u5e94\u5546\uff1a");
                panel9.add(label8);
                label8.setBounds(436, 36, 60, 25);

                //---- sNameTextField ----
                sNameTextField.setEditable(false);
                panel9.add(sNameTextField);
                sNameTextField.setBounds(495, 34, 97, 29);

                //---- label9 ----
                label9.setText("\u8054\u7cfb\u4eba\uff1a");
                panel9.add(label9);
                label9.setBounds(54, 99, label9.getPreferredSize().width, 19);

                //---- sPeopleTextField ----
                sPeopleTextField.setEditable(false);
                panel9.add(sPeopleTextField);
                sPeopleTextField.setBounds(110, 93, 119, 30);

                //---- label10 ----
                label10.setText("\u8054\u7cfb\u7535\u8bdd\uff1a");
                panel9.add(label10);
                label10.setBounds(265, 100, label10.getPreferredSize().width, 19);

                //---- phoneTextField ----
                phoneTextField.setEditable(false);
                panel9.add(phoneTextField);
                phoneTextField.setBounds(334, 95, 124, 30);

                //---- label11 ----
                label11.setText("\u5730\u5740\uff1a");
                panel9.add(label11);
                label11.setBounds(500, 100, label11.getPreferredSize().width, 19);

                //---- addressTextField ----
                addressTextField.setEditable(false);
                panel9.add(addressTextField);
                addressTextField.setBounds(541, 93, 127, 31);

                //---- label12 ----
                label12.setText("\u603b\u6570\u91cf\uff1a");
                panel9.add(label12);
                label12.setBounds(605, 40, label12.getPreferredSize().width, 18);

                //---- countTextField ----
                countTextField.setEditable(false);
                panel9.add(countTextField);
                countTextField.setBounds(660, 34, 66, countTextField.getPreferredSize().height);

                //---- label13 ----
                label13.setText("\u603b\u7801\u6d0b\uff1a");
                panel9.add(label13);
                label13.setBounds(new Rectangle(new Point(223, 150), label13.getPreferredSize()));

                //---- totalTextField ----
                totalTextField.setEditable(false);
                panel9.add(totalTextField);
                totalTextField.setBounds(275, 145, 70, totalTextField.getPreferredSize().height);

                //---- label14 ----
                label14.setText("\u603b\u5b9e\u6d0b\uff1a");
                panel9.add(label14);
                label14.setBounds(new Rectangle(new Point(396, 152), label14.getPreferredSize()));

                //---- realTextField ----
                realTextField.setEditable(false);
                panel9.add(realTextField);
                realTextField.setBounds(451, 147, 60, realTextField.getPreferredSize().height);

                //---- label15 ----
                label15.setText("\u4f18\u60e0\u91d1\u989d\uff1a");
                panel9.add(label15);
                label15.setBounds(new Rectangle(new Point(563, 153), label15.getPreferredSize()));

                //---- discountsTextField ----
                discountsTextField.setEditable(false);
                panel9.add(discountsTextField);
                discountsTextField.setBounds(628, 147, 65, discountsTextField.getPreferredSize().height);

                //---- label16 ----
                label16.setText("\u65e5\u671f\uff1a");
                panel9.add(label16);
                label16.setBounds(30, 147, label16.getPreferredSize().width, 22);

                //---- timeTextField ----
                timeTextField.setEditable(false);
                panel9.add(timeTextField);
                timeTextField.setBounds(75, 144, 105, timeTextField.getPreferredSize().height);

                {
                    // compute preferred size
                    Dimension preferredSize = new Dimension();
                    for(int i = 0; i < panel9.getComponentCount(); i++) {
                        Rectangle bounds = panel9.getComponent(i).getBounds();
                        preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                        preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                    }
                    Insets insets = panel9.getInsets();
                    preferredSize.width += insets.right;
                    preferredSize.height += insets.bottom;
                    panel9.setMinimumSize(preferredSize);
                    panel9.setPreferredSize(preferredSize);
                }
            }
            panel7.add(panel9, BorderLayout.CENTER);
        }
        contentPane.add(panel7);
        panel7.setBounds(0, 0, 738, panel7.getPreferredSize().height);

        //======== panel10 ========
        {
            panel10.setBorder(new TitledBorder("\u5165\u5e93\u5546\u54c1\u4fe1\u606f"));
            panel10.setLayout(null);

            //======== scrollPane1 ========
            {
                scrollPane1.setViewportView(table1);
            }
            panel10.add(scrollPane1);
            scrollPane1.setBounds(10, 20, 720, 228);

            {
                // compute preferred size
                Dimension preferredSize = new Dimension();
                for(int i = 0; i < panel10.getComponentCount(); i++) {
                    Rectangle bounds = panel10.getComponent(i).getBounds();
                    preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                    preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
                }
                Insets insets = panel10.getInsets();
                preferredSize.width += insets.right;
                preferredSize.height += insets.bottom;
                panel10.setMinimumSize(preferredSize);
                panel10.setPreferredSize(preferredSize);
            }
        }
        contentPane.add(panel10);
        panel10.setBounds(0, 250, 738, 259);

        contentPane.setPreferredSize(new Dimension(750, 585));
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel1;
    private JButton cancelButton;
    private JPanel panel7;
    private JPanel panel8;
    private JComboBox comboBox1;
    private JPanel panel9;
    private JLabel label6;
    private JTextField wareIDTextField;
    private JLabel label7;
    private JTextField sIDTextField;
    private JLabel label8;
    private JTextField sNameTextField;
    private JLabel label9;
    private JTextField sPeopleTextField;
    private JLabel label10;
    private JTextField phoneTextField;
    private JLabel label11;
    private JTextField addressTextField;
    private JLabel label12;
    private JTextField countTextField;
    private JLabel label13;
    private JTextField totalTextField;
    private JLabel label14;
    private JTextField realTextField;
    private JLabel label15;
    private JTextField discountsTextField;
    private JLabel label16;
    private JTextField timeTextField;
    private JPanel panel10;
    private JScrollPane scrollPane1;
    private JTable table1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
